package com.gip.controller;

import com.gip.dto.RecordInfo;
import com.gip.pojo.Auction;
import com.gip.pojo.AuctionCustomer;
import com.gip.pojo.Auctionrecord;
import com.gip.pojo.User;
import com.gip.service.AuctionService;
import com.gip.utils.AuctionPriceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class AuctionController {
    @Autowired
    private AuctionService auctionService;

    // 每页记录数
    private static final int PAGE_SIZE = 10;

    @RequestMapping("/queryAllAuctions")
    public ModelAndView queryAllAuctions(Model model, HttpSession session, @ModelAttribute("condition") Auction auction, BindingResult bindingResult,
                                         @RequestParam(required = false, defaultValue = "1", value = "pageNum") int pageNum) {
//        Object user = session.getAttribute("user");
//        if(user==null){
//            model.addAttribute("msg","还未登录");
//            return "login";
//        }

//        List<Auction> auctions = auctionService.queryAllAuctions(auction);
//        model.addAttribute("auctionList",auctions);
//        return "index";


        ModelAndView modelAndView = new ModelAndView();
        //分页处理
        PageHelper.startPage(pageNum, PAGE_SIZE);
        List<Auction> auctionList = this.auctionService.queryAllAuctions(auction);
        // 获取分页的数据
        PageInfo page = new PageInfo(auctionList);
        // 上一页
        page.getPrePage();
        // 下一页
        page.getNextPage();
        // 总记录数
        page.getTotal();
        // 获取总页码
        page.getPages();
        // 获取当前页码
        page.getPageNum();
        modelAndView.addObject("auctionList", auctionList);
        modelAndView.addObject("page", page);
        modelAndView.setViewName("index");
        return modelAndView;

    }

    @RequestMapping("/goAuctionDetail/{id}")
    public String goAuctionDetail(@PathVariable("id") int id, Model model) {
//        List<RecordInfo> records = auctionService.getRecords(id);
////        System.out.println(records);
//        model.addAttribute("records",records);
//        System.out.println();
//        return "detail";

        Auction auction = auctionService.selectAuctionDetailByAuctionId(id);
        model.addAttribute("auctionDetail", auction);
        return "auctionDetail";
    }

    @RequestMapping("saveAuctionRecord")
    public String saveAuctionRecord(Auctionrecord auctionrecord, HttpSession session) throws AuctionPriceException {
//        手动封装
        User user = (User) session.getAttribute("user");
        Integer userid = user.getUserid();
        auctionrecord.setUserid(userid);
        auctionrecord.setAuctiontime(new Date());
        auctionService.saveAuctionRecord(auctionrecord);
        return "redirect:/goAuctionDetail/" + auctionrecord.getAuctionid();
    }


    @RequestMapping(value = "/toAuctionResult")
    public ModelAndView toAuctionResult() {
        ModelAndView modelAndView = new ModelAndView();
        List<AuctionCustomer> endtimeList = this.auctionService.selectAuctionendtime();

        List<Auction> noendtimeList = this.auctionService.selectAuctionNoendtime();

        modelAndView.addObject("endtimeList", endtimeList);
        modelAndView.addObject("noendtimeList", noendtimeList);

        modelAndView.setViewName("auctionResult");

        return modelAndView;

    }

    // 跳转到发布拍卖品页面
    @RequestMapping(value = "/toAddAuctionPage")
    public String toAddAuctionPage() {
        return "addAuction";
    }

    // 发布商品实现
    @RequestMapping(value = "/publishAuctions")
    public String publishAuctions(Auction auction, MultipartFile pic) throws IOException {
        // 封装 图片的类型
        auction.setAuctionpictype(pic.getContentType());
        // 封装图片的名称
        //auction.setAuctionname(pic.getOriginalFilename());
        auction.setAuctionpic(pic.getOriginalFilename());
        File file = new File("E:\\pic\\" + pic.getOriginalFilename()+ UUID.randomUUID());
        // 上传
        pic.transferTo(file);
        this.auctionService.addAuction(auction);
        return "redirect:/queryAllAuctions";
    }

    //跳转修改拍卖品页面
    @RequestMapping("/goChange/{id}")
    public String goChange(@PathVariable("id") int id, Model model) {
        Auction auction = auctionService.selectAuctionDetailByAuctionId(id);
        model.addAttribute("auction", auction);
        return "change";
    }

    @RequestMapping("updateAuction")
    public String updateAuction(Auction auction, @RequestParam(required = false) MultipartFile newImg, HttpSession session) throws IOException {
        System.out.println(auction);
        if (newImg != null) {
            auction.setAuctionpic(newImg.getOriginalFilename());
            File file = new File("E:\\pic\\" + newImg.getOriginalFilename());
            //上传
            newImg.transferTo(file);
        }
        int i = auctionService.updateAuction(auction);
        if (i >= 1) {
            session.setAttribute("msg", "修改成功!");
        } else {
            session.setAttribute("msg", "修改失败!");
        }
        return "redirect:/goChange/" + auction.getAuctionid();
    }

    @PostMapping("/deleteAuction")
    @ResponseBody
    public Map deleteAuction(int auctionid){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println(auctionid);

        int i = auctionService.deleteByAuctionid(auctionid);
        if(i>=1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
}
